# LinearSearch.py # # Description: Linear search function version 1. # Requirement: We cannot use a Python conditional statement such as # "if x in list:" # # Author: Anne Lavergne # Date: April 2024 def linearSearch_v1(aList, aTarget): """Given a list of numbers and a target number we are looking for in the given list, this function returns True if the target number is in the given list of numbers, otherwise, it returns False. If there are duplicated instances of aTarget in aList, this function returns True once the first instance has matched aTarget. """ for element in aList: if element == aTarget: return True return False def linearSearch_v1_1(aList, aTarget): """Given a list of numbers and a target number we are looking for in the given list, this function returns True if the target number is in the given list of numbers, otherwise, it returns False. If there are duplicated instances of aTarget in aList, this function returns True once the first instance has matched aTarget. """ # using a while loop def linearSearch_v2(aList, aTarget): """Given a list of numbers and a target number we are looking for in the given list, this function returns True if the target number is in the given list of numbers, otherwise, it returns False. If there are duplicated instances of aTarget in aList, this function returns True once the last instance has matched aTarget. """ result = False for element in aList: if element == aTarget: result = True return result def linearSearch_v3(aList,aTarget): """Given a list of numbers and a target number we are looking for in the given list, this function returns the index of the element that first matches aTarget, otherwise, it returns None. """ result = None for index in range(len(aList)): if aList[index] == aTarget: result = index return result def linearSearch_v4(aList,aTarget): """Given a list of numbers and a target number we are looking for in the given list, this function returns the indices of all the elements that match aTarget, otherwise, it returns an empty list. """ matchList = [] for index in range(len(aList)): if aList[index] == aTarget: matchList.append(index) return matchList # ***Main part of my program # Test Case #1 theList = [ 4, 6, 8, 2, 4, 5, 3 ] theTarget = 4 theResult = linearSearch_v1(theList,theTarget) print(f"Calling linearSearch_v1({theList},{theTarget})") print("\tThe expected result is 'True'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v2(theList,theTarget) print(f"Calling linearSearch_v2({theList},{theTarget})") print("\tThe expected result is 'True'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v3(theList,theTarget) print(f"Calling linearSearch_v3({theList},{theTarget})") print("\tThe expected result is '4'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v4(theList,theTarget) print(f"Calling linearSearch_v4({theList},{theTarget})") print("\tThe expected result is '[ 0, 4 ]'.") print(f"\tThe actual result is: {theResult}") # Test Case #2 theList = [ 6, 6, 6, 6, 6, 6, 6 ] theTarget = 4 theResult = linearSearch_v1(theList,theTarget) print(f"Calling linearSearch_v1({theList},{theTarget})") print("\tThe expected result is 'False'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v2(theList,theTarget) print(f"Calling linearSearch_v2({theList},{theTarget})") print("\tThe expected result is 'False'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v3(theList,theTarget) print(f"Calling linearSearch_v3({theList},{theTarget})") print("\tThe expected result is 'None'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v4(theList,theTarget) print(f"Calling linearSearch_v4({theList},{theTarget})") print("\tThe expected result is '[ ]'.") print(f"\tThe actual result is: {theResult}") # Test Case #3 theList = [ 6, 6, 6, 6, 6, 6, 6 ] theTarget = 6 theResult = linearSearch_v1(theList,theTarget) print(f"Calling linearSearch_v1({theList},{theTarget})") print("\tThe expected result is 'True'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v2(theList,theTarget) print(f"Calling linearSearch_v2({theList},{theTarget})") print("\tThe expected result is 'True'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v3(theList,theTarget) print(f"Calling linearSearch_v3({theList},{theTarget})") print("\tThe expected result is '6'.") print(f"\tThe actual result is: {theResult}") theResult = linearSearch_v4(theList,theTarget) print(f"Calling linearSearch_v4({theList},{theTarget})") print("\tThe expected result is '[ 0, 1, 2, 3, 4, 5, 6 ]'.") print(f"\tThe actual result is: {theResult}")